/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.web.controller;

import com.opgea.qms.constraints.SessionConstraints;
import com.opgea.qms.dao.TokenDAO;
import com.opgea.qms.domain.entities.Company;
import com.opgea.qms.domain.entities.Employee;
import com.opgea.qms.domain.entities.Login;
import com.opgea.qms.domain.model.JsonModelMap;
import com.opgea.qms.domain.model.SessionData;
import com.opgea.qms.service.LoginService;
import com.opgea.qms.web.form.LoginDTO;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Ramesh
 */
@Controller
@RequestMapping(value="/login")
public class LoginController {
    
    
    @Autowired
    private LoginService loginService;
    
    @Autowired
    private TokenDAO tokenDAO;
    
    @RequestMapping(method= RequestMethod.GET)
    public String show(HttpServletRequest request){
        HttpSession session = request.getSession();
        String targetPage = "login";
        if(session.getAttribute(SessionConstraints.SESSION_DATA.name()) != null){
            targetPage = "redirect: desktop";
        }
        return targetPage;
    }
    
    @RequestMapping(value="verify", method= RequestMethod.POST)
    public @ResponseBody  Map<String, Object> isAuthenticUser(LoginDTO form, 
            HttpServletRequest request){
        

        Boolean authentic = loginService.isAuthenticUser(form.getLoginId(), form.getPassword());
        
        
        String success="Failed";
        if(authentic){
            success = "Success";
            HttpSession session  = request.getSession();
            Login login = loginService.find(form.getLoginId());
            Employee loggedUser = login.getEmployee();
            Company company = loggedUser.getCompany();
            
            SessionData sessionData = new SessionData();
            sessionData.setLoginId(login.getLoginId());
            sessionData.setEmpId(loggedUser.getId());
            sessionData.setEmpName((loggedUser.getFirstName()+" "+loggedUser.getMiddleInitial()+" "
                    + loggedUser.getLastName()).trim());
            sessionData.setCompanyId(company.getId());
            sessionData.setCompanyName(company.getName());
            if(loggedUser.getDestination() != null){
                sessionData.setDestinationId(loggedUser.getDestination().getId());
            }
            
            session.setAttribute(SessionConstraints.USER.toString(), login);
            session.setAttribute(SessionConstraints.COMPANY_ID.toString(), loggedUser.getCompany().getId());
            session.setAttribute(SessionConstraints.SESSION_DATA.toString(), sessionData);
            
            if(loggedUser.getDestination() != null){
                session.setAttribute(SessionConstraints.EMPLOYEE_ALLOTED_DESTINATION.toString(), 
                loggedUser.getDestination().getId());
            }
        }
        
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("success", true);
        dataMap.put("data", success);
        dataMap.put("errormsg","Could not login.");
        return dataMap;
    }
    
    @RequestMapping(value="loginInfo", method= RequestMethod.GET)
    public @ResponseBody 
    Map<String, Object> getLoginInfo(HttpServletRequest request){
        System.out.println("Get Login Info...");
        HttpSession session = request.getSession();
        SessionData sessionData = (SessionData) session.getAttribute(SessionConstraints.SESSION_DATA.toString());
        return JsonModelMap.success().data(sessionData);
    }
    
    @RequestMapping(value="logout", method= RequestMethod.GET)
    public @ResponseBody 
    Map<String, Object> logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        SessionData sessionData = (SessionData) session.getAttribute(SessionConstraints.SESSION_DATA.name());
        session.invalidate();
        return JsonModelMap.success().data("login");
    }
    
    @RequestMapping(value="isExistingUser", method= RequestMethod.GET)
    public @ResponseBody 
    Map<String, Object> isExistingUser(@RequestParam(value="emailId", required=false)String emailId, HttpServletRequest request){
        
        if(loginService.find(emailId) != null){
            return JsonModelMap.success().data("YES");
        }else{
            return JsonModelMap.success().data("NO");
        }
    }
}
